package com.example.securitymy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.securitymy.domain.ResponseResult;
import com.example.securitymy.domain.User;
import com.example.securitymy.mapper.UserMapper;
import com.example.securitymy.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public ResponseResult register(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();

        // 检查邮箱是否已存在
        queryWrapper.eq(User::getEmail, user.getEmail());
        User existingUser = userMapper.selectOne(queryWrapper);

        if (existingUser != null) {
            return new ResponseResult(400, "用户已存在");
        }

        // 加密密码并保存用户
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        user.setDelFlag(0); // 设置删除标志为未删除
        user.setStatus("0"); // 设置账号状态为正常
        userMapper.insert(user);

        return new ResponseResult(200, "注册成功");
    }
}